{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pymysql\n",
    "#建立连接\n",
    "conn = pymysql.connect(\n",
    "    host = '127.0.0.1',\n",
    "    user = 'root',\n",
    "    password = 'yt',\n",
    "    database = 'test',\n",
    "    charset = 'utf8'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "dtype: int64\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "myvar = pd.Series(a)\n",
    "#说明这个是我们的 Series是一个列\n",
    "print(myvar)\n",
    "#通过下标获取元素\n",
    "print(myvar[2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x    Google\n",
      "y    Runoob\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "a = [\"Google\", \"Runoob\", \"Wiki\"]\n",
    "myvar = pd.Series(a, index=[\"x\", \"y\", \"z\"])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "1      你好\n2    1231\ndtype: object"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vars= pd.Series({\"1\":\"你好\",\"2\":\"1231\"})\n",
    "vars"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10.0\n",
      "1    12.0\n",
      "2    13.0\n",
      "Name: Age, dtype: float64\n",
      "===--===\n",
      "     Site   Age\n",
      "0  Google  10.0\n",
      "1  Runoob  12.0\n",
      "2    Wiki  13.0\n"
     ]
    }
   ],
   "source": [
    "# DataFrame是由多个Serise组合而成的\n",
    "data = [['Google', 10], ['Runoob', 12], ['Wiki', 13]]\n",
    "\n",
    "#\n",
    "df = pd.DataFrame(data,\n",
    "                  columns=['Site', 'Age'], dtype=float)\n",
    "\n",
    "#获取指定的列的数据\n",
    "print(df[\"Age\"])\n",
    "print(\"===--===\")\n",
    "#获取指定行的数据 指定的行数\n",
    "print(df.loc[0:4])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      calories  duration\n",
      "day1       420        50\n",
      "day2       380        40\n",
      "day3       390        45\n",
      "calories    420\n",
      "duration     50\n",
      "Name: day1, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"calories\": [420, 380, 390],\n",
    "    \"duration\": [50, 40, 45]\n",
    "}\n",
    "#index 指定索引的\n",
    "ss = pd.DataFrame(data=data, index=[\"day1\", \"day2\", \"day3\"])\n",
    "# print(ss[\"calories\"])\n",
    "#获取指定的行数是多少\n",
    "print(ss)\n",
    "print(ss.loc[\"day1\"])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      3\n",
      "1      3\n",
      "2    NaN\n",
      "3      1\n",
      "4      3\n",
      "5    NaN\n",
      "6      2\n",
      "7      1\n",
      "8     na\n",
      "Name: NUM_BEDROOMS, dtype: object\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3    False\n",
      "4    False\n",
      "5     True\n",
      "6    False\n",
      "7    False\n",
      "8    False\n",
      "Name: NUM_BEDROOMS, dtype: bool\n",
      "           PID  ST_NUM    ST_NAME OWN_OCCUPIED NUM_BEDROOMS NUM_BATH SQ_FT\n",
      "0  100001000.0   104.0     PUTNAM            Y            3        1  1000\n",
      "1  100002000.0   197.0  LEXINGTON            N            3      1.5    --\n",
      "8  100009000.0   215.0    TREMONT            Y           na        2  1800\n"
     ]
    }
   ],
   "source": [
    "# pds = pd.read_sql('select * from tbl_book', con=conn)\n",
    "#\n",
    "# #判断某一列是否是空值\n",
    "# print(pds[\"id\"].isnull())\n",
    "# #判断\n",
    "# # # print(pds.head(2))\n",
    "# # #查看指定的行数\n",
    "# # print(pds.head(2))\n",
    "# # #读取末尾2行\n",
    "# # print(pds.tail(2))\n",
    "\n",
    "#读取csv 文件\n",
    "#na_values\n",
    "datas = pd.read_csv('../data/property-data.csv', na_values=[])\n",
    "print(datas[\"NUM_BEDROOMS\"])\n",
    "print(datas[\"NUM_BEDROOMS\"].isnull())\n",
    "new_df = datas.dropna()\n",
    "print(new_df)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     name    age\n",
      "0  Google     50\n",
      "1  Runoob     40\n",
      "2  Taobao  12345\n"
     ]
    },
    {
     "data": {
      "text/plain": "0    False\n1    False\n2    False\nName: name, dtype: bool"
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person = {\n",
    "    \"name\": ['Google', 'Runoob', 'Taobao'],\n",
    "    \"age\": [50, 40, 12345]  # 12345 年龄数据是错误的\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(person)\n",
    "print(df)\n",
    "#指定行 之后指定列\n",
    "# df.loc[2, 'age'] = 30  # 修改数据\n",
    "# print(df)\n",
    "df[\"name\"].isnull()\n",
    "# print(df.to_string())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}